//
//  ZMAutoScrollLabelController.m
//  testDemo
//
//  Created by 赵林瑞 on 16/6/21.
//  Copyright © 2016年 RZOL. All rights reserved.
//

#import "ZMAutoScrollLabelController.h"



static NSString * const reuseCellID = @"AutoScrollLabelCell";

@interface ZMAutoScrollLabelController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) int count;

@end



@implementation ZMAutoScrollLabelController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 300, kScreeWith-40, 200) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (CADisplayLink *)displayLink {
    if (!_displayLink) {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
        self.count = 0;
    }
    return _displayLink;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    [self startAnimation];
}

- (void)startAnimation{
    //self.beginTime = CACurrentMediaTime();
    self.displayLink.paused = NO;
}
- (void)stopAnimation{
    self.displayLink.paused = YES;
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self stopAnimation];
}

- (void)handleDisplayLink:(CADisplayLink *)link {
    
    self.count ++;
    
    NSLog(@"%d", self.count);
    
    CGFloat step = (200/240.f) * self.count;
    
    [self.tableView setContentOffset:CGPointMake(0, 0 + step)];
    
    if (self.count >= 240) {
        self.count = 0;
    }
    
}

- (void)setupTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = [UIColor orangeColor];
    
    self.dataArr = [NSMutableArray array];
    [self.dataArr addObjectsFromArray:self.textArr];
    
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark -- tableView的协议方法 --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCellID];
    }
    
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.textLabel.text = self.dataArr[indexPath.row];
    
    return cell;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
